package com.tiger.cloud.controller.rongyun;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tiger.cloud.common.mybatis.BaseController;
import com.tiger.cloud.common.rongyun.models.TokenResult;
import com.tiger.cloud.dao.sms.SmsGroupDao;
import com.tiger.cloud.filter.CurrentUserId;
import com.tiger.cloud.filter.IgnoreAuth;
import com.tiger.cloud.model.crm.User;
import com.tiger.cloud.model.ext.SmsUserRequest;
import com.tiger.cloud.model.sms.SmsGroup;
import com.tiger.cloud.model.sms.SmsGroupUsers;
import com.tiger.cloud.service.rongyun.RongYunApiService;
import com.tiger.cloud.util.ErrorCode;
import com.tiger.cloud.util.Page;
import com.tiger.cloud.util.Response;
import com.tiger.cloud.util.ResponseUtil;
import com.tiger.cloud.util.StringUtil;


@Controller
@RequestMapping(value = "rongYun")
@Api(value = "融云接口", tags = "融云接口")
public class RongyunApiController extends BaseController {
    @Autowired
    private RongYunApiService rongYunApiService;
    @Autowired
    private SmsGroupDao smsGroupDao;
    
    @RequestMapping(value = "getRongToken", method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin
    @IgnoreAuth
    @ApiOperation(value = "获取Token",notes = "获取Token")
    public Response getToken(@RequestBody User user) throws Exception {
        TokenResult tokenResult =  rongYunApiService.getRYToken(user);
      if(tokenResult == null){
          return ResponseUtil.error("获取token失败!");
      }
        return ResponseUtil.ok(tokenResult);
    }

    @RequestMapping(value = "refreshRongToken", method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin
    @IgnoreAuth
    @ApiOperation(value = "刷新用户融云信息",notes = "刷新用户融云信息")
    public Response refreshToken(@RequestBody User user) throws Exception {
        if(!rongYunApiService.refreshToken(user)){
            return ResponseUtil.error("刷新用户信息失败!");
        }
        return ResponseUtil.ok(true);
    }
    
    @RequestMapping(value = "test", method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value = "测试创建群组方法",notes = "创建群组方法")
    public Response test(@ApiParam(name="id",value="用户id",hidden=true)@CurrentUserId Long userId,@RequestBody SmsUserRequest userRequest) throws Exception {
        return ResponseUtil.ok(rongYunApiService.getGroup(userRequest.getGroupId()));
    }
    
    @RequestMapping(value = "createGroup", method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value = "创建群组方法",notes = "创建群组方法")
    public Response createGroup(@ApiParam(name="id",value="用户id",hidden=true)@CurrentUserId Long userId,@RequestBody SmsUserRequest userRequest) throws Exception {
    	if (StringUtil.isEmpty(userRequest.getGroupName())) {
			return ResponseUtil.error(ErrorCode.OBJECT_NOT_EXITS,"groupName参数必传!");
		}
        if(!rongYunApiService.createGroup(userId, userRequest.getGroupName(),userRequest.getImage(),userRequest.getRemark())){
            return ResponseUtil.error("创建群组失败!");
        }
        return ResponseUtil.ok(true);
    }
    
    
    @RequestMapping(value = "getGroup", method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value = "查询群组信息方法",notes = "查询群组信息方法")
    public Response getGroup(@ApiParam(name="id",value="用户id",hidden=true)@CurrentUserId Long userId,@RequestBody SmsUserRequest userRequest) throws Exception {
    	if (userRequest.getGroupId()==null) {
			return ResponseUtil.error(ErrorCode.OBJECT_NOT_EXITS,"groupId参数必传!");
		}
        SmsGroup smsGroup=rongYunApiService.getGroup(userRequest.getGroupId());
        System.out.println(smsGroup.getUserId()+"     "+userId);
        if(smsGroup!=null&&smsGroup.getUserId().longValue()==userId.longValue()){
        	smsGroup.setHasUserCreated(true);
        }
        return ResponseUtil.ok(smsGroup);
    }

    @RequestMapping(value = "dismissGroup", method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value = "解散群组方法",notes = "解散群组方法")
    public Response dismissGroup(@ApiParam(name="id",value="用户id",hidden=true)@CurrentUserId Long userId,@RequestBody SmsUserRequest userRequest) throws Exception {
    	if (userRequest.getGroupId()==null) {
			return ResponseUtil.error(ErrorCode.OBJECT_NOT_EXITS,"GroupId参数必传!");
		}
        if(!rongYunApiService.dismissGroup(userId, userRequest.getGroupId())){
            return ResponseUtil.error("解散群组失败!");
        }
        return ResponseUtil.ok(true);
    }
    
    @RequestMapping(value = "quitGroup", method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value = "退出群组方法",notes = "退出群组方法")
    public Response quitGroup(@ApiParam(name="id",value="用户id",hidden=true)@CurrentUserId Long userId,@RequestBody SmsUserRequest userRequest) throws Exception {
    	if (userRequest.getGroupId()==null) {
			return ResponseUtil.error(ErrorCode.OBJECT_NOT_EXITS,"GroupId参数必传!");
		}
        if(!rongYunApiService.quitGroup(userId, userRequest.getGroupId())){
            return ResponseUtil.error("解散群组失败!");
        }
        return ResponseUtil.ok(true);
    }

    @RequestMapping(value = "joinGroup", method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value = "加入群组方法",notes = "加入群组方法")
    public Response joinGroup(@RequestBody SmsUserRequest userRequest) throws Exception {
    	if (userRequest.getGroupId()==null) {
			return ResponseUtil.error(ErrorCode.OBJECT_NOT_EXITS,"userId参数必传!");
		}
    	if (userRequest.getGroupId()==null) {
			return ResponseUtil.error(ErrorCode.OBJECT_NOT_EXITS,"groupId参数必传!");
		}
        if(!rongYunApiService.joinGroup(userRequest.getUserId(), userRequest.getGroupId())){
            return ResponseUtil.error("已经在群组!");
        }
        return ResponseUtil.ok(true);
    }
    
    @RequestMapping(value = "refreshGroup", method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin
    @IgnoreAuth
    @ApiOperation(value = "刷新群组信息方法",notes = "刷新群组信息方法")
    public Response refreshGroup(@ApiParam(name="id",value="用户id",hidden=true)@CurrentUserId Long userId,@RequestBody SmsUserRequest userRequest) throws Exception {
    	if (userRequest.getGroupId()==null) {
			return ResponseUtil.error(ErrorCode.OBJECT_NOT_EXITS,"GroupId参数必传!");
		}
        if(!rongYunApiService.refreshGroup(userRequest.getGroupId())){
            return ResponseUtil.error("刷新群组信息失败!");
        }
        return ResponseUtil.ok(true);
    }
    
    @RequestMapping(value = "queryGroupUsers", method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value = "分页查询群成员方法",notes = "分页查询群成员方法")
    public Response queryGroupUsers(@ApiParam(name="id",value="用户id",hidden=true)@CurrentUserId Long userId,@RequestBody SmsUserRequest userRequest) throws Exception {
    	if (userRequest.getGroupId()==null) {
			return ResponseUtil.error(ErrorCode.OBJECT_NOT_EXITS,"GroupId参数必传!");
		}
    	List<User> refreshResult = rongYunApiService.queryGroupUsers(userRequest.getGroupId());
        if(refreshResult == null){
            return ResponseUtil.error("分页查询群成员失败!");
        }
        return ResponseUtil.ok(refreshResult);
    }
    
    @RequestMapping(value = "queryGroup", method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value = "分页查询用户拥有的群方法",notes = "分页查询用户拥有的群方法")
    public Response queryGroup(@ApiParam(name="id",value="用户id",hidden=true)@CurrentUserId Long userId,@RequestBody SmsGroup smsGroup) throws Exception {
    	smsGroup.setUserId(userId);
    	List<SmsGroup> refreshResult = rongYunApiService.queryGroup(smsGroup);
        if(refreshResult == null){
            return ResponseUtil.error("分页查询用户拥有的群失败!");
        }
        return ResponseUtil.ok(refreshResult);
    }
    

    @RequestMapping(value = "updateGroup", method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value = "修改群资料",notes = "修改群资料")
    public Response updateGroup(@ApiParam(name="id",value="用户id",hidden=true)@CurrentUserId Long userId,@RequestBody SmsGroup smsGroup) throws Exception {
    	if (smsGroup.getId()==null) {
			return ResponseUtil.error(ErrorCode.OBJECT_NOT_EXITS,"GroupId参数必传!");
		}
        return ResponseUtil.ok(rongYunApiService.updateGroup(smsGroup));
    }

    @RequestMapping(value = "queryPeerUsers", method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value = "邀请用户列表",notes = "邀请用户列表")
    public Response queryPeerUsers(@ApiParam(name="id",value="用户id",hidden=true)@CurrentUserId Long userId,@RequestBody SmsUserRequest userRequest) throws Exception {
    	if (userRequest.getGroupId()==null) {
			return ResponseUtil.error(ErrorCode.OBJECT_NOT_EXITS,"GroupId参数必传!");
		}
    	SmsGroupUsers smsGroupUsers=new SmsGroupUsers();
    	smsGroupUsers.setUserId(userId);
    	Page<User> refreshResult = rongYunApiService.queryGroupUsers(userRequest.getGroupId(),smsGroupUsers);
        return ResponseUtil.ok(refreshResult);
    }
    
}
